#!/bin/sh
#
# Configure script for mythplugins
#

# 
#    default parameters for all plugins
#

prefix="/usr/local"
libdir_name="lib"

iplayer="yes"
opengl="yes"

cc="gcc"



if test x"$1" = x"-h" -o x"$1" = x"--help" ; then
cat << EOF

Usage:   configure [options]
Options: [defaults in brackets after descriptions]
NB:      all --enable-* options can be reversed with --disable-*

Generic options:
  --prefix=PREFIX          MythTV install location PREFIX [$prefix]"
  --libdir-name=LIBNAME    install/look for libs in PREFIX/LIBNAME [$libdir_name]"
  --help                   print this message
  --enable-all             Enable all options
  --enable-opengl          enable OpenGL (Music and Gallery) [$opengl]
  --previous               use previous configure parameters if possible

EOF
exit 1
fi

if [ -e config.log ] ; then
    PCONF=`cat config.log | tail -n 1 | grep configure`
    if test x"$PCONF" != x"" ; then
        PCONF=`echo $PCONF | sed 's/.\/configure//g'`
    fi
    for opt do
        if test x"$opt" != x"--prev" -a x"$opt" != x"--previous" ; then
            OTHOPT="$OTHOPT$opt"
        fi
        if test x"$opt" = x"--prev" -o x"$opt" = x"--previous" ; then
            if test x"$PCONF" != "--prev" ; then
                PREV="yes";
            fi
        fi
    done
    if test x"$PREV" = x"yes"; then
        echo "Options: $PCONF $OTHOPT"
        ./configure $PCONF $OTHOPT
        exit
    fi
fi

CONFIGURATION_OPTS=""
for opt do
  CONFIGURATION_OPTS="$CONFIGURATION_OPTS ""$opt"
done

date >> config.log
echo "   $0$CONFIGURATION_OPTS" >> config.log

for opt do
  case "$opt" in
  --prefix=*) prefix=`echo $opt | cut -d '=' -f 2`
  ;;
  --libdir-name=*) libdir_name=`echo $opt | cut -d '=' -f 2`;
  ;;
  --enable-opengl) opengl="yes"
  ;;
  --disable-opengl) opengl="no"
  ;;
  esac
done

# set temporary file name
if test ! -z "$TMPDIR" ; then
    TMPDIR1="${TMPDIR}"
elif test ! -z "$TEMPDIR" ; then
    TMPDIR1="${TEMPDIR}"
else
    TMPDIR1="/tmp"
fi

TMPC="${TMPDIR1}/myth-comp-${RANDOM}-$$-${RANDOM}.c"
TMPE="${TMPDIR1}/myth-conf-${RANDOM}-$$-${RANDOM}"


has_library()
{
    if test -f /etc/ld.so.conf ; then
        LIBPATHS=`cat /etc/ld.so.conf | grep -v "#" | grep -v "include"`
    else
        LIBPATHS=''
    fi

    LIBPATHS="$LIBPATHS `echo $LD_LIBRARY_PATH | sed s/':'/' '/g` "
    # Mac OS X has a different variable for this:
    LIBPATHS="$LIBPATHS `echo $DYLD_LIBRARY_PATH | sed s/':'/' '/g` "

    # recurse down just one level, should be enough in most cases
    if test -f /etc/ld.so.conf ; then
        INCL=`cat /etc/ld.so.conf | grep -v "#" | grep  "^include"`
        SRCH=`echo "$INCL" | sed '{ s/^include \//\//; s/^include /\/etc\//}'`
        for x in $SRCH ; do
            LP_APPEND=`cat $x | grep -v "#" | grep -v "^include"`
            LIBPATHS="$LIBPATHS $LP_APPEND"
        done
    fi

    LIBPATHS="`echo $LIBPATHS $prefix/$libdir_name /$libdir_name /usr/$libdir_name /usr/local/$libdir_name /usr/X11R6/$libdir_name | sed s/'\/ '/' '/g` "

    HAS_IT="no"
    for LIBPATH in $LIBPATHS ; do
        if test x`ls $LIBPATH/$1* 2> /dev/null | head -n 1` != x"" ; then
            HAS_IT="yes"
        fi
    done
    expr $HAS_IT : "yes" > /dev/null
}

has_header()
{
    HPATHS="$prefix/include /usr/local/include /usr/include /usr/include/g++-v3 /usr/X11R6/include /"
    INCL=`echo $DYLD_LIBRARY_PATH $LD_LIBRARY_PATH | sed s/':'/' '/g`
    HPATHS="$HPATHS `echo $INCL | sed s/$libdir_name/include/g` "

    HAS_IT="no"
    for HPATH in $HPATHS ; do
        if test x`ls $HPATH/$1* 2> /dev/null | head -n 1` != x"" ; then
            HAS_IT="yes"
        fi
    done
    expr $HAS_IT : "yes" > /dev/null
}

if test "$opengl" != "no" ; then
    opengl="no"
    if has_library libGL ; then
        opengl="yes"
    fi
    if test -f /System/Library/Frameworks/AGL.framework/Versions/A/AGL ; then
        opengl="yes"
    fi
fi

cat > $TMPC << EOF
#include <stdint.h>
int main( void ) { return 0; }
EOF

_stdint_h=no
if $cc -o $TMPE $TMPC 2> /dev/null ; then
  _stdint_h=yes
fi

rm -f $TMPC $TMPE

###########################################################
#                                                         #
#  Top level mythiplayer.pro file creation                 #
#  (ie. which plugins to build)                           #
#                                                         #
###########################################################

# bring in mythtv config
if [ -e $prefix/include/mythtv/mythconfig.mak ] ; then
  rm mythconfig.mak 2> /dev/null
  ln -s $prefix/include/mythtv/mythconfig.mak mythconfig.mak
else
  echo "ERROR: mythconfig.mak not found at $prefix/include/mythtv/mythconfig.mak"
  echo "Did you make AND install MythTV first?"
  echo "Are you using the correct prefix ($prefix)?"
  echo "Bailing out!!"
  exit
fi

echo ""
echo "Configuration settings: "
echo " "

echo "#" > ./config.pro
echo "#    Automatically generated by mythplugin configure" >> ./config.pro
echo "#    (manual modifications will be overwritten)" >> ./config.pro
echo "#" >> ./config.pro

if test "$iplayer" = "yes" ; then
  echo "        MythiPlayer     plugin will be built"
  #echo "SUBDIRS += mythiplayer i18n theme" >> ./config.pro
  #if test "$opengl" = "yes" ; then
      #echo "        OpenGL         support will be included in MythiPlayer"
      #echo "#define OPENGL_SUPPORT 1" >> config.pro
      #echo "CONFIG += opengl" >> config.pro
      #echo "HEADERS += glsingleview.h" >> config.pro
      #echo "SOURCES += glsingleview.cpp" >> config.pro
  #fi

else
  echo "        MythiPlayer     plugin will not be built"
fi


###########################################################
#                                                         #
#   An extra directory and Makefile for cleanup rules     #
#                                                         #
###########################################################

mkdir -p cleanup
cat << END_CLEANUP > cleanup/Makefile
all:
clean:
distclean:
install:
qmake_all:
	\$(NONE)

# Hack to remove some empty directories that qmake doesn't create rules for
uninstall:
#	-rmdir \$(INSTALL_ROOT)/$prefix/share/mythtv/*
#	-rmdir \$(INSTALL_ROOT)/$prefix/share/mythtv

END_CLEANUP

echo >> ./config.pro
#echo "# clean up for 'make uninstall'. This must be last" >> ./config.pro
#echo "SUBDIRS += cleanup " >> ./config.pro

###########################################################

echo ""

if test -x $QTDIR/bin/qmake; then
  $QTDIR/bin/qmake QMAKE=${QTDIR}/bin/qmake mythiplayer.pro
else
  qmake mythiplayer.pro
fi
